package com.lqq.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.lqq.dao.NewsinfoDAO;
import com.lqq.entity.Newsinfo;

public class NewsinfoDAOImpl implements NewsinfoDAO {

	SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Newsinfo> getAllNewsinfoByPage(int curPage, int pageSize) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Newsinfo.class);
		
		criteria.setFirstResult( ( curPage - 1 ) * pageSize );
		criteria.setMaxResults(pageSize);
		criteria.addOrder(Order.desc("createDate"));
		
		return criteria.list();
	}

	@Override
	public Integer getCountOfAllNewsinfo() {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Newsinfo.class);
		return criteria.list().size();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Newsinfo> getNewsinfoByConditionAndPage(Newsinfo condition, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Newsinfo.class);
		if(condition != null){
			if(condition.getTitle() != null && !("").equals(condition.getTitle())){
				criteria.add(Restrictions.like("title", condition.getTitle(),MatchMode.ANYWHERE));
			}
			if(condition.getTopic() != null && condition.getTopic().getId() != null){
				criteria.add(Restrictions.eq("topic.id", condition.getTopic().getId()));
			}
		}
		
		criteria.setFirstResult( ( curPage - 1 ) * pageSize );
		criteria.setMaxResults(pageSize);
		criteria.addOrder(Order.desc("createDate"));
		
		return criteria.list();
	}

	@Override
	public Integer getCountOfNewsinfoByCondition(Newsinfo condition) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Newsinfo.class);
		if(condition != null){
			if(condition.getTitle() != null && !("").equals(condition.getTitle())){
				criteria.add(Restrictions.like("title", condition.getTitle(),MatchMode.ANYWHERE));
			}
			if(condition.getTopic() != null && condition.getTopic().getId() != null){
				criteria.add(Restrictions.eq("topic.id", condition.getTopic().getId()));
			}
		}

		return criteria.list().size();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Newsinfo> getNewsinfoByTopicType(Integer id) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Newsinfo where topic.id="+id+" order by createDate desc");
		
		query.setMaxResults(5);
		return query.list();
	}

	@Override
	public Newsinfo getNewsinfoById(Integer id) {
		// TODO Auto-generated method stub
		
		Session session = sessionFactory.getCurrentSession();
		return (Newsinfo) session.get(Newsinfo.class, id);
	}

	@Override
	public boolean saveNewsinfo(Newsinfo newsinfo) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		try {
			session.save(newsinfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean updateNewsinfo(Newsinfo newsinfo) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		try {
			session.update(newsinfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean deleteNewsinfo(Newsinfo newsinfo) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		try {
			session.delete(newsinfo);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
